package com.common.bean;

import java.io.*;

/**
 * 拆分文件
 */
public class SplitFileInfo {

    private String fileName;
    private Long fragmentNum;
    private int fragmentSize;
    private Long LastFragmentSize;
    private Long totalSize;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Long getFragmentNum() {
        return fragmentNum;
    }

    public void setFragmentNum(Long fragmentNum) {
        this.fragmentNum = fragmentNum;
    }

    public int getFragmentSize() {
        return fragmentSize;
    }

    public void setFragmentSize(int fragmentSize) {
        this.fragmentSize = fragmentSize;
    }

    public Long getLastFragmentSize() {
        return LastFragmentSize;
    }

    public void setLastFragmentSize(Long lastFragmentSize) {
        LastFragmentSize = lastFragmentSize;
    }

    public Long getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(Long totalSize) {
        this.totalSize = totalSize;
    }

    //拆分文件名，拆分份数，拆分大小，最后一次拆分大小，被拆分文件总大小
    public static SplitFileInfo info(String msg) {
        try {

            SplitFileInfo splitFileInfo = new SplitFileInfo();
            String[] split = msg.split(",");
            if (split.length == 5) {
                splitFileInfo.setFileName(split[0]);
                splitFileInfo.setFragmentNum(Long.parseLong(split[1]));
                splitFileInfo.setFragmentSize(Integer.parseInt(split[2]));
                splitFileInfo.setLastFragmentSize(Long.parseLong(split[3]));
                splitFileInfo.setTotalSize(Long.parseLong(split[4]));
            } else {
                //拆分文件失败
                System.out.println("你接收的文件，拆分说明有问题");
                return null;
            }
            return splitFileInfo;
        } catch (Exception e) {
            return null;
        }
    }

    public static SplitFileInfo info(File file) {

        try {
            SplitFileInfo splitFileInfo = new SplitFileInfo();
            String[] split = getString(file).split(",");
            if (split.length == 5) {
                splitFileInfo.setFileName(split[0]);
                splitFileInfo.setFragmentNum(Long.parseLong(split[1]));
                splitFileInfo.setTotalSize(Long.parseLong(split[4]));
                splitFileInfo.setFragmentSize(Integer.parseInt(split[2]));
                splitFileInfo.setLastFragmentSize(Long.parseLong(split[3]));
            } else {
                //拆分文件失败
                System.out.println("你接收的文件，拆分说明有问题");
                return null;
            }
            return splitFileInfo;
        } catch (Exception e) {
            return null;
        }
    }


    private static String getString(File file) {
        InputStreamReader inputStreamReader = null;
        try {
            InputStream inputStream = new FileInputStream(file);
            inputStreamReader = new InputStreamReader(inputStream, "utf-8");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        StringBuffer sb = new StringBuffer("");
        BufferedReader reader = new BufferedReader(inputStreamReader);
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            return null;
        }
        return sb.toString();

    }
}
